
package com.lb.jwitter.action.report;


import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiam;
import com.lb.jwitter.service.TblQLDTTXAHocVienMienGiamService;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienPhieuDangKyMonHocReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

   
    private Long hocKyId;
    
    private String report;    
    
    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    
    
    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService;
    private TblQLDTTXAHocVienPhieuDangKyMonHocService tblQLDTTXAHocVienPhieuDangKyMonHocService;
    private TblQLDTTXAHocVienMienGiamService tblQLDTTXAHocVienMienGiamService;
    
    
    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;
    private   List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens;
    private   List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs;
    private List<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs;


  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_hocPhi", "xem")){ 
        return "noprivilege"; 
    } 
 
        //tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
        
//        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
//        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 


        msg = new Message(); 
        
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 20, 1);
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "xem")){ 
         return "noprivilege"; 
     } 
  
//         tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient");
//         tblQLDTTXADMKhoaHocService = (TblQLDTTXADMKhoaHocService) context.getBean("tbl_QLDTTXA_DM_KhoaHocClient");
//         tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DMKhoaClient");
//         tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");
//         tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
         tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient");
         tblQLDTTXAHocVienPhieuDangKyMonHocService = (TblQLDTTXAHocVienPhieuDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_PhieuDangKyMonHocClient"); 
         tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient");
         tblQLDTTXAHocVienMienGiamService = (TblQLDTTXAHocVienMienGiamService) context.getBean("tbl_QLDTTXA_HocVien_MienGiamClient");

         
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
         /*
          * Lay ra cac mon hoc duoc mo trong hoc ky
          */
         cons.clear();
         if(hocKyId != null && !hocKyId.equals(new Long(0))){ 
         	
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocKyId.id");
             con.setValue1(hocKyId);
             con.setOperator("eq");
             cons.add(con);	
         }
         tblQLDTTXALopMonHocDuKiens = tblQLDTTXALopMonHocDuKienService.findtblQLDTTXALopMonHocDuKienByCondition(cons, 0, 0);
         
         tblQLDTTXAMonHocs = new ArrayList<TblQLDTTXAMonHoc>();
         List<Long> monHocIds = new ArrayList<Long>();
         for(TblQLDTTXALopMonHocDuKien tblLop : tblQLDTTXALopMonHocDuKiens){
      	   if(!monHocIds.contains(tblLop.getMonHocId().getId())){
     		   tblQLDTTXAMonHocs.add(tblLop.getMonHocId());
     		   monHocIds.add(tblLop.getMonHocId().getId());
     	   }
         	
         }
         
         //lay cac thong tin hoc phi cua hoc vien
         cons.clear();
         if(hocKyId != null && !hocKyId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocKyId.id");
             con.setValue1(hocKyId);
             con.setOperator("eq");
             cons.add(con);	
         } 

         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.trangThaiId.id");
             con.setValue1(new Long(1));
             con.setOperator("eq");
             cons.add(con);	
         }
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hocVienId.hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 

         tblQLDTTXAHocVienPhieuDangKyMonHocs = tblQLDTTXAHocVienPhieuDangKyMonHocService.findtblQLDTTXAHocVienPhieuDangKyMonHocByCondition(cons, 0, 0);
	      
 		
	     	      
		  //sinh report
		  int colNo = 0;
		  
		  report ="";
		  int stt = 0;
		  DecimalFormat myFormatter = new DecimalFormat("###,###.##");
		  
		  for(TblQLDTTXAHocVienPhieuDangKyMonHoc hocVienPhieuDangKyMonHoc : tblQLDTTXAHocVienPhieuDangKyMonHocs){ 
			   
			   cons.clear();
			   if(hocVienPhieuDangKyMonHoc != null){ 
			       ConditionReport con = new ConditionReport(); 
			       con.setCol("phieuDangKyId.id");
			       con.setValue1(hocVienPhieuDangKyMonHoc.getId());
			       con.setOperator("eq");
			       cons.add(con);	
			   } 
		
    	      List<TblQLDTTXAHocVienDangKyMonHoc> tblQLDTTXAHocVienDangKyMonHocs = tblQLDTTXAHocVienDangKyMonHocService.findtblQLDTTXAHocVienDangKyMonHocByCondition(cons, 0, 0);
		      DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
		      
		      stt++;
		      report += "<tr class='tableitem' > "; 
		      //stt
		      report += "<TD>" +stt+ "</TD>"; 
		      //mshv
		      report += "<td>" + hocVienPhieuDangKyMonHoc.getHocVienId().getMSHV()+ "</td> ";
		      //lop
		      report += "<td>" +hocVienPhieuDangKyMonHoc.getHocVienId().getLopId().getTen()+ "</td> ";
		      //ho ten
		      report += "<td>" +hocVienPhieuDangKyMonHoc.getHocVienId().getHoTen()+ "</td> ";
		      report += "<td>" +hocVienPhieuDangKyMonHoc.getHocVienId().getTen()+ "</td> ";
		      //ngay sinh
		      try{
			      report += "<td>" 
			    	  +hocVienPhieuDangKyMonHoc.getHocVienId().getHoSoId().getNgaySinh().getDay()+"/"
			    	  +hocVienPhieuDangKyMonHoc.getHocVienId().getHoSoId().getNgaySinh().getMonth()+"/"
			    	  +hocVienPhieuDangKyMonHoc.getHocVienId().getHoSoId().getNgaySinh().getYear()+ "</td>";
			      
		      }catch(Exception e){
		    	  report += "<td></td>"; 
		            
		      }
		      
		      cons.clear();
			   { 
			       ConditionReport con = new ConditionReport(); 
			       con.setCol("donMGId.hocVienId.id");
			       con.setValue1(hocVienPhieuDangKyMonHoc.getHocVienId().getId());
			       con.setOperator("eq");
			       cons.add(con);	
			   } 
			   { 
			       ConditionReport con = new ConditionReport(); 
			       con.setCol("duocMienGiam");
			       con.setValue1(true);
			       con.setOperator("eq");
			       cons.add(con);	
			   } 
			   List<TblQLDTTXAHocVienMienGiam> tblQLDTTXAHocVienMienGiams = tblQLDTTXAHocVienMienGiamService.findtblQLDTTXAHocVienMienGiamByCondition(cons, 0, 0);
		      
		      //hoc phi tung mon hoc
		      for(TblQLDTTXAMonHoc monHoc : tblQLDTTXAMonHocs){
			      Boolean b = false;
			      Boolean mienMon = false;
				   Double hocPhiMonHoc = 0.0;
				   for(TblQLDTTXAHocVienDangKyMonHoc chiTietDangKyMonHoc : tblQLDTTXAHocVienDangKyMonHocs){
						
					   if(chiTietDangKyMonHoc.getMonHocId().getId().equals(monHoc.getId())){
					       b = true;
					       //mienMon = false;
					       for(TblQLDTTXAHocVienMienGiam mienGiam : tblQLDTTXAHocVienMienGiams){
					    	   if (chiTietDangKyMonHoc.getMonHocId().getId().equals(mienGiam.getMonHocId().getId())){
					    		   mienMon = true;
					    		   break;
					    	   }
					       }
					       
					       break;
					   }
				   }//end for chi tiet hoc phi
				   if(b){
					   if(mienMon)
						   report += "<TD>MM</TD>";
					   else
						   report += "<TD>" +monHoc.getMaMonHoc()+ "</TD>";
					   
				   }else
					   report += "<TD></TD>";
				   
				   
				   
				   
			   }//end-for mon hoc

			   
			   
			   report +=  "</tr> ";
			   
			   
		  }

		  
  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXABienNhanHoSo Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}


public Long getHocKyId() {
	return hocKyId;
}
public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
}

public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
	return tblQLDTTXAHocKyTrongNams;
}
public void setTblQLDTTXAHocKyTrongNams(
		List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams) {
	this.tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNams;
}
public List<TblQLDTTXALopMonHocDuKien> getTblQLDTTXALopMonHocDuKiens() {
	return tblQLDTTXALopMonHocDuKiens;
}
public void setTblQLDTTXALopMonHocDuKiens(
		List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens) {
	this.tblQLDTTXALopMonHocDuKiens = tblQLDTTXALopMonHocDuKiens;
}
public List<TblQLDTTXAMonHoc> getTblQLDTTXAMonHocs() {
	return tblQLDTTXAMonHocs;
}
public void setTblQLDTTXAMonHocs(List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs) {
	this.tblQLDTTXAMonHocs = tblQLDTTXAMonHocs;
}
public void setMsg(Message msg) {
	this.msg = msg;
} 
 } 
 
